package View;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.CloseAction;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.Window;

import javax.swing.JTextArea;
import javax.swing.JToolBar;
import java.awt.Component;
import javax.swing.JButton;
import javax.swing.ImageIcon;

import Bussiness.Update_Action;
import Model.Users;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import java.awt.Dimension;
import java.awt.GridLayout;

public class SuaNguoiDung extends JFrame {
    Users newUser =null;
    /** Creates new form SuaKhoaHoc */
    public SuaNguoiDung(Users user) {
    	super("Sửa thông tin người dùng");
    	setAlwaysOnTop(true);
    	getContentPane().setMaximumSize(new Dimension(2000000000, 2147483647));
    	setResizable(false);
    	newUser = user;
    	setIconImage(Toolkit.getDefaultToolkit().getImage(SuaKhoaHoc.class.getResource("/Icon/QLSV.jpg")));
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        initComponents();
        setLocation(520,20);
        getContentPane().setLayout(new GridLayout(0, 1, 0, 0));
     
        txtTaiKhoan.setText(newUser.getTaiKhoan());
        txtMatKhau.setText(newUser.getMatKhau());
        txtMatKhauXacNhan.setText(newUser.getMatKhau());
        txtTenXacNhan.setText(newUser.getTenXacNhan());
        getContentPane().add(jPanel1);
        pack();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        btn_CapNhap = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        txtTaiKhoan = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtMatKhau = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jPanel1.setBackground(SystemColor.textHighlight);
        jToolBar1.setBackground(SystemColor.inactiveCaption);
        jToolBar1.setFloatable(false);

        btn_CapNhap.setBackground(new java.awt.Color(227, 239, 249));
        btn_CapNhap.setFont(new java.awt.Font("Times New Roman", 1, 13));
        btn_CapNhap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Sua.png")));
        btn_CapNhap.setText("Cập Nhật");
        btn_CapNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CapNhapActionPerformed(evt);
            }
        });

        jToolBar1.add(btn_CapNhap);

        jLabel3.setFont(new Font("Times New Roman", Font.BOLD, 13));
        jLabel3.setText("Tài khoản:");

        txtTaiKhoan.setFont(new java.awt.Font("Times New Roman", 0, 13));
        txtTaiKhoan.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        jLabel4.setFont(new Font("Times New Roman", Font.BOLD, 13));
        jLabel4.setText("Mật khẩu:");

        txtMatKhau.setFont(new java.awt.Font("Times New Roman", 0, 13));
        txtMatKhau.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        
        JLabel lblMtKhuXc = new JLabel("Mật khẩu xác nhận:");
        lblMtKhuXc.setFont(new Font("Times New Roman", Font.BOLD, 12));
        
        txtMatKhauXacNhan = new JTextField();
        txtMatKhauXacNhan.setFont(new Font("Times New Roman", Font.PLAIN, 13));
        txtMatKhauXacNhan.setColumns(10);
        
        lblTnXcNhn = new JLabel("Tên xác nhận:");
        lblTnXcNhn.setFont(new Font("Times New Roman", Font.BOLD, 12));
        
        txtTenXacNhan = new JTextField();
        txtTenXacNhan.setFont(new Font("Times New Roman", Font.PLAIN, 13));
        txtTenXacNhan.setColumns(10);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1Layout.setHorizontalGroup(
        	jPanel1Layout.createParallelGroup(Alignment.TRAILING)
        		.addGroup(jPanel1Layout.createSequentialGroup()
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.TRAILING)
        				.addComponent(jSeparator1, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE)
        				.addComponent(jToolBar1, GroupLayout.DEFAULT_SIZE, 406, Short.MAX_VALUE))
        			.addGap(0))
        		.addGroup(Alignment.LEADING, jPanel1Layout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
        				.addComponent(lblTnXcNhn, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblMtKhuXc, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
        				.addComponent(jLabel4, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
        				.addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE))
        			.addGap(15)
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
        				.addComponent(txtTaiKhoan, GroupLayout.PREFERRED_SIZE, 261, GroupLayout.PREFERRED_SIZE)
        				.addComponent(txtMatKhau, 261, 261, 261)
        				.addComponent(txtMatKhauXacNhan, 261, 261, 261)
        				.addComponent(txtTenXacNhan, GroupLayout.PREFERRED_SIZE, 261, GroupLayout.PREFERRED_SIZE))
        			.addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
        	jPanel1Layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(jPanel1Layout.createSequentialGroup()
        			.addComponent(jToolBar1, GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addComponent(jSeparator1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        			.addPreferredGap(ComponentPlacement.UNRELATED)
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        				.addComponent(txtTaiKhoan, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel4)
        				.addComponent(txtMatKhau, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(lblMtKhuXc, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        				.addComponent(txtMatKhauXacNhan, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
        			.addGap(18)
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(txtTenXacNhan, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        				.addComponent(lblTnXcNhn))
        			.addGap(37))
        );
        jPanel1Layout.linkSize(SwingConstants.VERTICAL, new Component[] {txtTaiKhoan, txtMatKhau, txtMatKhauXacNhan, txtTenXacNhan});
        jPanel1Layout.linkSize(SwingConstants.HORIZONTAL, new Component[] {txtTaiKhoan, txtMatKhau, txtMatKhauXacNhan, txtTenXacNhan});
        jPanel1.setLayout(jPanel1Layout);
        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void btn_CapNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_

    	String tenTaiKhoan = txtTaiKhoan.getText().trim();
		String matKhau = txtMatKhau.getText().trim();
		String xacNhanMatKhau = txtMatKhauXacNhan.getText().trim();
		String tenXacNhan = txtTenXacNhan.getText().trim();
		 

		if ("".equalsIgnoreCase(tenTaiKhoan)) {
			JOptionPane.showMessageDialog(null, "Chưa nhập tên tài khoản!",
					"Null Values !", JOptionPane.ERROR_MESSAGE);
		} else if (matKhau.equalsIgnoreCase("")) {
			JOptionPane.showMessageDialog(null, "Chưa nhập mật khẩu!",
					"Null Values !", JOptionPane.ERROR_MESSAGE);
		}else if (xacNhanMatKhau.equalsIgnoreCase("")) {
			JOptionPane.showMessageDialog(null, "Chưa nhập xác nhận mật khẩu!",
					"Null Values !", JOptionPane.ERROR_MESSAGE);
		}else if (tenXacNhan.equalsIgnoreCase("")) {
			JOptionPane.showMessageDialog(null, "Chưa nhập tên xác nhận!",
					"Null Values !", JOptionPane.ERROR_MESSAGE);
		}else if (!matKhau.equals(xacNhanMatKhau)) {
			JOptionPane.showMessageDialog(null, "Mật khẩu xác nhận không trùng!",
					"Null Values !", JOptionPane.ERROR_MESSAGE);
		} else {
			Users newTaiKhoan = new Users();
			newTaiKhoan.setTaiKhoan(tenTaiKhoan);
			newTaiKhoan.setMatKhau(matKhau);
			newTaiKhoan.setTenXacNhan(tenXacNhan);
			newTaiKhoan.setId(newUser.getId());
			Update_Action ua = new Update_Action();
			if (ua.SuaTaiKhoan(newTaiKhoan) == true) {
				int j = JOptionPane.showConfirmDialog(null,
						"Sửa thành công! Bạn có muốn tiếp tục sửa không?",
						"Continue ... !", 2);
				if (j != 0)
					this.dispose();
			} else {
				JOptionPane.showMessageDialog(this, "Không sửa được!");
			}
			ua.Close();
		}
		QuanLy.Add_DanhSachUser();
    }//GEN-LAST:event_btn_CapNhapActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_CapNhap;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField txtMatKhau;
    private javax.swing.JTextField txtTaiKhoan;
    private JTextField txtMatKhauXacNhan;
    private JLabel lblTnXcNhn;
    private JTextField txtTenXacNhan;
}
